package org.example.src.controller;

import org.example.src.dto.request.JobPostRequest;
import org.example.src.dto.response.ApiResponse;
import org.example.src.dto.response.JobPostResponse;
import org.example.src.entity.JobPost;
import org.example.src.service.JobPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/job-posts")
@CrossOrigin(origins = "*")
public class JobPostController extends BaseController {

    @Autowired
    private JobPostService jobPostService;

    @GetMapping
    public ResponseEntity<ApiResponse<List<JobPost>>> getAllJobPosts() {
        List<JobPost> jobPosts = jobPostService.findAllJobPosts();
        return success(jobPosts);
    }

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<JobPost>> getJobPostById(@PathVariable Long id) {
        JobPost jobPost = jobPostService.findJobPostById(id);
        if (jobPost == null) {
            return notFound("职位未找到");
        }
        return success(jobPost);
    }

    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'COMPANY')")
    public ResponseEntity<ApiResponse<JobPost>> createJobPost(@Valid @RequestBody JobPostRequest request) {
        JobPost createdJobPost = jobPostService.createJobPost(request);
        return created(createdJobPost);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'COMPANY')")
    public ResponseEntity<ApiResponse<JobPost>> updateJobPost(@PathVariable Long id, @Valid @RequestBody JobPostRequest request) {
        JobPost updatedJobPost = jobPostService.updateJobPost(id, request);
        return success("职位更新成功", updatedJobPost);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'COMPANY')")
    public ResponseEntity<ApiResponse<Void>> deleteJobPost(@PathVariable Long id) {
        jobPostService.deleteJobPost(id);
        return noContent();
    }

    @GetMapping("/company/{companyId}")
    public ResponseEntity<ApiResponse<List<JobPost>>> getJobPostsByCompanyId(@PathVariable Long companyId) {
        List<JobPost> jobPosts = jobPostService.findJobPostsByCompanyId(companyId);
        return success(jobPosts);
    }

    @GetMapping("/active")
    public ResponseEntity<ApiResponse<List<JobPost>>> getActiveJobPosts() {
        List<JobPost> jobPosts = jobPostService.findActiveJobPosts();
        return success(jobPosts);
    }

    @GetMapping("/search")
    public ResponseEntity<ApiResponse<List<JobPost>>> searchJobPosts(@RequestParam String keyword) {
        List<JobPost> jobPosts = jobPostService.searchJobPosts(keyword);
        return success(jobPosts);
    }

    @GetMapping("/approved-companies")
    public ResponseEntity<ApiResponse<List<JobPost>>> getActiveJobsFromApprovedCompanies() {
        List<JobPost> jobPosts = jobPostService.findActiveJobsFromApprovedCompanies();
        return success(jobPosts);
    }

    @GetMapping("/active-public")
    public ResponseEntity<ApiResponse<List<JobPostResponse>>> getActiveJobPostsPublic() {
        List<JobPostResponse> jobPosts = jobPostService.getActiveJobPosts();
        return success(jobPosts);
    }
}
